//
//  HWReadHabitSettingView.m
//  嗨文
//
//  Created by 响当当 on 5/6/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWReadHabitSettingView.h"

@interface HWReadHabitSettingView()
@property (nonatomic, strong)UILabel *boyOrGirLabel;
@property (nonatomic, strong)UILabel *subTitle;
@property (nonatomic, strong)UIButton *restHabbitBtn;//重置button
@end

@implementation HWReadHabitSettingView


- (UIButton *)restHabbitBtn
{
    if (!_restHabbitBtn)
    {
        _restHabbitBtn = [HWUtilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-65*kUIScaleBasePlus,SCREEN_HEIGHT/2+22*kUIScaleBasePlus,130*kUIScaleBasePlus,44*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentCenter
                                                     text:@"重制"
                                                 textClor:LCHexColor(0xffffff)
                                                buttonBGC:LCHexColor(0xF54337)
                                                 textFont:17*kUIScaleBasePlus
                                                   target:self
                                                   method:@selector(restHabbit)];
        _restHabbitBtn.layer.masksToBounds = YES;
        _restHabbitBtn.layer.cornerRadius = 4;
    }
    return _restHabbitBtn;
}

- (UILabel *)boyOrGirLabel
{
    if (!_boyOrGirLabel)
    {
        UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(0,35*kUIScaleBasePlus,SCREEN_WIDTH,60*kUIScaleBasePlus)
                                                 textAligment:NSTextAlignmentCenter
                                                     textClor:LCHexColor(0x4C5159)
                                                     textFont:24*kUIScaleBasePlus];
        titleLabel.text = @"您已设置阅读偏好";
        [self addSubview:titleLabel];
        
        _boyOrGirLabel = [HWUtilties creatLabelWithFrame:CGRectMake(37*kUIScaleBasePlus,CGRectGetMaxY(titleLabel.frame),SCREEN_WIDTH-30*kUIScaleBasePlus,50*kUIScaleBasePlus)
                                                  textAligment:NSTextAlignmentLeft
                                                      textClor:LCHexColor(0x4C5159)
                                                      textFont:18*kUIScaleBasePlus];

    }
    return _boyOrGirLabel;
}

- (UILabel *)subTitle
{
    if (!_subTitle)
    {
        _subTitle = [HWUtilties creatLabelWithFrame:CGRectMake(37*kUIScaleBasePlus, CGRectGetMaxY(self.boyOrGirLabel.frame),SCREEN_WIDTH-30*kUIScaleBasePlus,50*kUIScaleBasePlus)
                           textAligment:NSTextAlignmentLeft
                               textClor:LCHexColor(0x4C5159)
                               textFont:18*kUIScaleBasePlus];
        _subTitle.text = @"您已选择的阅读标签：";
    }
    return _subTitle;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.boyOrGirLabel];
        [self addSubview:self.subTitle];
        [self addSubview:self.restHabbitBtn];
    }
    return self;
}

- (void)creatUI:(NSDictionary *)dic boyHabit:(NSArray *)boyArr girlHabit:(NSArray *)girlArr
{
    NSString *readHabitTaste = [NSString stringWithFormat:@"您选择的阅读口味：%@",[dic[@"readTaste"] integerValue] ==1 ? @"男" : @"女" ];
    self.boyOrGirLabel.attributedText = [readHabitTaste addAttriButedString:readHabitTaste
                                                                      Color:LCHexColor(0xF54337)
                                                                       Font:[UIFont systemFontOfSize:18*kUIScaleBasePlus]
                                                                       rang:NSMakeRange(readHabitTaste.length-1,1)];
    
    
    NSString *tasteSeries = IFNULL(dic[@"tasteSeries"],[NSString string]);
    NSArray *arr = [tasteSeries componentsSeparatedByString:@","];
    NSMutableArray *numArr = [[NSMutableArray alloc]init];
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *str = obj;
        if (idx == 0)
        {
            NSString *numStr = [str substringFromIndex:1];
            [numArr addObject:numStr];
        }
        else if(idx == arr.count-1)
        {
            NSString *numStr = [str substringWithRange:NSMakeRange(1,str.length-2)];
            [numArr addObject:numStr];
        }
        else
        {
            [numArr addObject:str];
        }
    }];
    
    CGFloat width = 90*kUIScaleBasePlus;
    CGFloat height = 37*kUIScaleBasePlus;
    
    NSMutableArray *readHabitArr = [[NSMutableArray alloc]init];
    
    if ([dic[@"readTaste"] integerValue] == 1)
    {
        [boyArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *dic = obj;
            [numArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString *numStr = numArr[idx];
                if (numStr.integerValue == [dic[@"seriesId"] integerValue])
                {
                    [readHabitArr addObject:dic[@"seriesName"]];
                }
            }];
        }];
    }
    else
    {
        [girlArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *dic = obj;
            [numArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString *numStr = numArr[idx];
                if (numStr.integerValue == [dic[@"seriesId"] integerValue])
                {
                    [readHabitArr addObject:dic[@"seriesName"]];
                }
            }];
        }];
    }
    
    
    for (int i=0; i<readHabitArr.count; i++)
    {
        UIButton *btn = [HWUtilties creatButtonWithFrame:CGRectMake(37*kUIScaleBasePlus+(width+15*kUIScaleBasePlus)*(i%3),CGRectGetMaxY(self.subTitle.frame)+(height+15*kUIScaleBasePlus)*(i/3),width,height)
                                            textAligment:NSTextAlignmentCenter
                                                    text:readHabitArr[i]
                                                textClor:LCHexColor(0xF54337)
                                               buttonBGC:LCHexColor(0xFFF5F5)
                                                textFont:16*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(btnClick)];
        btn.enabled = NO;
        [self addSubview:btn];
    }
   
    
}


- (void)restHabbit
{
    if (self.getBackReadHabbitBlock)
    {
        self.getBackReadHabbitBlock();
    }
}

//无效
- (void)btnClick
{
    
}
@end
